﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SharpSvn;
using SharpSvn.UI;
using System.IO;


namespace LiveProjectManager.Controllers
{
    public class RepositoryController : Controller
    {
        protected static DDD.Services.ConfigService _config = new DDD.Services.ConfigService();

        //
        // GET: /Repository
        public ActionResult Index()
        {
            ContentResult myJson = new ContentResult();
            myJson.ContentType = "application/json";
            myJson.Content = getRepo();
            return myJson;
        }

        //
        // Get: /Repository/getContentOfFile
        public string getContentOfFile(string path)
        {
            return System.IO.File.ReadAllText(path.Replace("/", "\\"));
        }

        private string getRepo()
        {
            string appData = Path.Combine(
                Server.MapPath("~/Content"), 
                (string)_config.getByKey("REPOSITORY_LOCAL_PATH").Value
            );

            checkRepo((string)_config.getByKey("REPOSITORY_URL").Value, appData);

            return createJson(appData);
        }

        private void checkRepo(string path, string dir)
        {
            SvnClient svnClient = new SvnClient();
            SvnUriTarget target = new SvnUriTarget(path);

            try
            {
                if (!Directory.Exists(dir))
                    svnClient.CheckOut(target, dir);
                else
                    svnClient.Update(dir);
            }
            catch (SvnException) 
            {
                svnClient.CleanUp(dir);
                svnClient.CheckOut(target, dir);
            }
        }

        private string createJson(string dir)
        {
            return "[" + populateJson(dir) + "]";
        }

        private string populateJson(string dir)
        {
            DirectoryInfo directory = new DirectoryInfo(dir);
            string Json = "";


            DirectoryInfo[] directories = directory.GetDirectories();
            foreach (DirectoryInfo d in directories)
            {
                if (!d.Name.StartsWith("."))
                {
                    Json += "[{ \"data\" : \"";
                    Json += d.Name;
                    Json += "\", \"children\" : [";
                    Json += populateJson(d.FullName);
                    Json += "]}]";
                    if (!directories.Last().Equals(d))
                        Json += ",";
                }
            }

            FileInfo[] files = directory.GetFiles();

            if (directories.Length != 0 && files.Length != 0)
                Json += " , ";

            foreach (FileInfo f in files)
            {
                if(f.Name.EndsWith(".txt"))
                    Json += " {\"data\" : \"" + f.Name + "\", \"metadata\" : { \"href\" : \"" + (f.FullName).Replace("\\", "/") + "\"}}";
                else
                    Json += " \"" + f.Name + "\" ";
                if (!files.Last().Equals(f))
                    Json += ",";
            }

            return Json;
        }
    }
}